package com.ztgf.order.api.client;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.order.models.bo.OrderAfterSaleUpdateBO;
import com.ztgf.order.models.bo.OrderReturnCommodityListBO;
import com.ztgf.order.models.bo.OrderReturnCommoditySaveBO;
import com.ztgf.order.models.bo.OrderReturnCommodityUpdateBO;
import com.ztgf.order.models.bo.pay.PayOrderSaveBO;
import com.ztgf.order.models.dto.OrderReturnCommodityDTO;
import com.ztgf.order.models.service.IOrderAfterSaleLogService;
import com.ztgf.order.models.service.IOrderReturnCommodityService;
import com.ztgf.order.models.service.async.IAsyncOrderService;
import com.ztgf.order.models.service.common.ICommonService;
import com.ztgf.order.models.vo.OrderReturnCommodityDetailsVO;
import com.ztgf.order.models.vo.OrderReturnCommodityListVO;
import com.ztgf.order.models.vo.OrderUpdateAfterSaleDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-06-30 10:37
 * Description:订单退换货表控制器
 */
@RestController
@RequestMapping("/api/orderReturnCommodity")
@Slf4j
@Api(value = "OrderReturnCommodityApiController", tags = "订单退换货表Api接口")
public class OrderReturnCommodityApiController {


    @DubboReference
    private IOrderReturnCommodityService orderReturnCommodityService;
    @DubboReference
    private IOrderAfterSaleLogService orderAfterSaleLogService;
    @DubboReference
    private ICommonService commonService;
    @DubboReference
    private IAsyncOrderService asyncOrderService;

    @PostMapping(value = "/initiateReturnCommodity")
    @ApiOperation(value = "发起退款/退换货接口", notes = "发起退款/退换货接口")
    public Result initiateReturnCommodity(OrderReturnCommoditySaveBO saveBO) {
        return orderReturnCommodityService.add(saveBO);
    }

    @PostMapping(value = "/updateIsCancel")
    @ApiOperation(value = "取消售后订单接口", notes = "取消售后订单接口")
    public Result updateIsCancel(Long id) {
        if (id != null) {
            return orderReturnCommodityService.updateIsCancel(id);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/updateAuditState")
    @ApiOperation(value = "卖家同意/不同意接口", notes = "卖家同意/不同意接口")
    public Result updateAuditState(Long id, Integer type) {
        if (id != null) {
            return orderReturnCommodityService.updateAuditState(id, type);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }


    @PostMapping(value = "/updateSendMeg")
    @ApiOperation(value = "卖家给买家发送发货地址，拒绝理由接口", notes = "卖家给买家发送发货地址，拒绝理由接口")
    public Result updateSendMeg(OrderReturnCommodityUpdateBO updateBO) {
        if (updateBO.getId() != null) {
            return orderReturnCommodityService.updateSendMeg(updateBO);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }


    @PostMapping(value = "/updateSignFo")
    @ApiOperation(value = "卖家签收退款/签收换货接口", notes = "卖家签收退款/签收换货接口")
    public Result updateSignFo(Long id) {
        Result result = new Result();
        try{
            if(id == null){
                return new Result(CodeEnum.FAIL_100002);
            }
            OrderReturnCommodityDTO dto = orderReturnCommodityService.selectById(id);
            if(dto == null){
                return new Result(CodeEnum.FAIL_100002);
            }
            if(dto.getAfterSalesState() == 3){
                return new Result(CodeEnum.AFTER_SALE_ORDER_ERROR_320002);
            }
            //签收退款
            if (dto.getType() == 0) {
                result = commonService.saveReturnReceive(dto);
            } else {
                boolean forReturn = orderReturnCommodityService.SellerSignForReturn(dto);
                if (!forReturn) {
                    return new Result(CodeEnum.AFTER_SALE_ORDER_ERROR_320000);
                }
            }

        } catch (ServiceException e){
            log.error("退款签收异常ServiceException: " + id, e);
            result.setCode(e.getCode());
            result.setMsg(e.getMessage());
        } catch (RuntimeException e){
            log.error("退款签收异常RuntimeException: " + id, e);
            result.setCode(CodeEnum.FAIL.getCode());
            result.setMsg("签收失败");
        } catch (Exception e){
            log.error("退款签收异常Exception: " + id, e);
            result.setCode(CodeEnum.FAIL.getCode());
            result.setMsg("签收失败");
        }
        return result;
    }

    @PostMapping(value = "/updateBuyersSignFo")
    @ApiOperation(value = "买家签收接口", notes = "买家签收接口")
    public Result updateBuyersSignFo(Long id) {

        Result result = new Result();
        try{
            if(id == null){
                return new Result(CodeEnum.FAIL_100002);
            }
            OrderReturnCommodityDTO dto = orderReturnCommodityService.selectById(id);
            if(dto == null){
                return new Result(CodeEnum.FAIL_100002);
            }
            if(dto.getAfterSalesState() == 3){
                return new Result(CodeEnum.AFTER_SALE_ORDER_ERROR_320002);
            }
            result = orderReturnCommodityService.updateBuyersSignFo(id);
            if(result.success()){
                log.error("---买家签收换货 解冻金额、支付佣金------>");
                PayOrderSaveBO payOrderSaveBO = new PayOrderSaveBO();
                payOrderSaveBO.setType(13);
                payOrderSaveBO.setIds(dto.getOrderId().toString());
                payOrderSaveBO.setPayType(0);
                asyncOrderService.savePay(payOrderSaveBO, false);
            }

        } catch (ServiceException e){
            log.error("买家签收换货异常ServiceException: " + id, e);
            result.setCode(e.getCode());
            result.setMsg(e.getMessage());
        } catch (RuntimeException e){
            log.error("买家签收换货异常RuntimeException: " + id, e);
            result.setCode(CodeEnum.FAIL.getCode());
            result.setMsg("签收失败");
        } catch (Exception e){
            log.error("买家签收换货异常Exception: " + id, e);
            result.setCode(CodeEnum.FAIL.getCode());
            result.setMsg("签收失败");
        }
        return result;
    }


    @PostMapping(value = "/updateInterventionState")
    @ApiOperation(value = "买家申请平台介入接口", notes = "买家申请平台介入接口")
    public Result updateInterventionState(Long id) {
        if (id != null) {
            return orderReturnCommodityService.updateInterventionState(id);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/updateIsState")
    @ApiOperation(value = "平台判定后，买家卖家同意与否操作接口", notes = "平台判定后，买家卖家同意与否操作接口")
    public Result updateIsState(Long id, Integer isState, Integer type) {
        if (id != null) {
            return orderReturnCommodityService.updateIsState(id, isState, type);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/queryPageList")
    @ApiOperation(value = "个人中心我的售后列表接口", notes = "个人中心我的售后列表接口")
    public Result queryPageList(OrderReturnCommodityListBO bo) {
        Page<OrderReturnCommodityListVO> pageList = orderReturnCommodityService.queryPageList(bo);
        return new Result<>(pageList);
    }


    @PostMapping(value = "/getDetailsById")
    @ApiOperation(value = "查询售后订单退换货详情", notes = "查询售后订单退换货详情")
    public Result getDetailsById(Long id) {
        if (id != null) {
            OrderReturnCommodityDetailsVO details = orderReturnCommodityService.getDetailsById(id);
            return new Result<>(details);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/querySchedule")
    @ApiOperation(value = "获取售后订单进度跟踪", notes = "获取售后订单进度跟踪")
    public Result querySchedule(Long id) {
        if (id != null) {
            return new Result<>(orderAfterSaleLogService.querySchedule(id));
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/updateOrderAfterSale")
    @ApiOperation(value = "修改售后信息", notes = "修改售后信息")
    public Result updateOrderAfterSale(OrderAfterSaleUpdateBO bo) {
        if (null == bo.getId()) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            return orderReturnCommodityService.updateOrderAfterSale(bo);
        }
    }

    @PostMapping(value = "/getUpdateOrderAfterSaleById")
    @ApiOperation(value = "根据售后订单id查询售后修改信息", notes = "根据售后订单id查询售后修改信息")
    public Result getUpdateOrderAfterSaleById(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            OrderUpdateAfterSaleDetailsVO vo = orderReturnCommodityService.getUpdateOrderAfterSaleById(id);
            return new Result<>(vo);
        }
    }

    @PostMapping(value = "/afterSalesQuantityToBeProcessed")
    @ApiOperation(value = "获取店铺信息页待处理售后数量", notes = "获取店铺信息页待处理售后数量")
    public Result afterSalesQuantityToBeProcessed(Long shopId) {
        if (null == shopId) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Integer num = orderReturnCommodityService.afterSalesQuantityToBeProcessed(shopId);
        return new Result<>(num);
    }


}
